﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAL.Repositories;
using Model;
using Model.Admin;
using Type = Model.Type;

namespace Web.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        public ActionResult Index()
        {
            var apartmentRep = new ApartmentRepository();
            var apartments = apartmentRep.ReadAll();

            return View(apartments);
        }

        //
        // GET: /Admin/Filter
        public ActionResult Filter(string filterCode)
        {
            var apartmentRep = new ApartmentRepository();
            var apartments = apartmentRep.ReadByCode(filterCode) ?? new List<ApartmentListViewModel>();
            return View("Index", apartments);
        }

        //
        // GET: /Admin/FilterModel
        public ActionResult FilterModel(string filterModel)
        {
            var apartmentRep = new ApartmentRepository();
            IList<ApartmentListViewModel> apartments;

            if(filterModel != String.Empty)
            {
                apartments = apartmentRep.ReadByModel(filterModel) ?? new List<ApartmentListViewModel>();
            }
            else
            {
                apartments = new List<ApartmentListViewModel>();
            }
             
            return View("Index", apartments);
        }

        //
        // GET: /Admin/NewApartment/
        public ActionResult NewApartment()
        {
            var codeListRep = new CodeListRepository();

            //var states = codeListRep.ReadStates().ToList();
            
            var floors = codeListRep.ReadFloors().ToList();
            floors.RemoveAt(0);

            var roomsNumbers = codeListRep.ReadRoomsNumber().ToList();
            roomsNumbers.RemoveAt(0);

            var facades = codeListRep.ReadFacades().ToList();
            
            var viewModel = new ApartmentCrudViewModel()
            {
                //States = states, 
                Floors = floors, 
                RoomsNumbers = roomsNumbers,
                Facades = facades
            };

            return View(viewModel);
        }

        //
        // POST: /Admin/NewApartment/
        [HttpPost]
        public ActionResult NewApartment(ApartmentCrudViewModel model)
        {
            var codeListRep = new CodeListRepository();
            //var selectedState = (State) codeListRep.Read(model.SelectedState);
            var selectedFloor = (Floor) codeListRep.Read(model.SelectedFloor);
            var selectedRoomsNumber = (RoomsNumber) codeListRep.Read(model.SelectedRoomsNumber);
            var selectedFacade = (Facade)codeListRep.Read(model.SelectedFacade);
            
            if (ModelState.IsValid)
            {
                var apartment = new Apartment()
                {
                    Code = model.Code,
                    Building = model.Building,
                    State = codeListRep.ReadStates()[2],
                    Floor = selectedFloor,
                    RoomsNumber = selectedRoomsNumber,
                    Facade = selectedFacade,
                    Model = (int) model.ApartmentModel,
                    Price = (double) model.Price,
                    Area = (double ) model.Area
                };

                var indexTable = new ApartmentSearch()
                {
                    Code = model.Code,
                    Floor = codeListRep.Read(model.SelectedFloor).Name,
                    RoomsNumber = codeListRep.Read(model.SelectedRoomsNumber).Name,
                    Facade = codeListRep.Read(model.SelectedFacade).Name,
                    Apartment = apartment
                };

                apartment.IndexTable = indexTable;
                var apartmentRep = new ApartmentRepository();
                apartmentRep.Create(apartment);

                return RedirectToAction("Index");
            }

            #region get dropdownlist values

            //var states = codeListRep.ReadStates().ToList();
            
            var floors = codeListRep.ReadFloors().ToList();
            floors.RemoveAt(0);

            var roomsNumbers = codeListRep.ReadRoomsNumber().ToList();
            roomsNumbers.RemoveAt(0);

            var facades = codeListRep.ReadFacades().ToList();

            #endregion

            //model.States = states;
            model.Floors = floors;
            model.RoomsNumbers = roomsNumbers;
            model.Facades = facades;

            return View(model);
        }

        // GET: /Admin/DeleteApartment/5
        public ActionResult DeleteApartment(int id)
        {
            return View();
        }

        // brisanje članka
        // POST: /Admin/DeleteApartment/5
        [HttpPost]
        public ActionResult DeleteApartment(int id, FormCollection collection)
        {
            var apartmentRep = new ApartmentRepository();
            apartmentRep.Delete(id);

            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/EditApartment/5
        public ActionResult EditApartment(int id)
        {
            var codeListRep = new CodeListRepository();

            var states = codeListRep.ReadStates().ToList();
            
            var floors = codeListRep.ReadFloors().ToList();
            floors.RemoveAt(0);

            var roomsNumbers = codeListRep.ReadRoomsNumber().ToList();
            roomsNumbers.RemoveAt(0);

            var facades = codeListRep.ReadFacades().ToList();

            var apartmentRep = new ApartmentRepository();
            var apartment = apartmentRep.ReadAdmin(id);

            var viewModel = new ApartmentCrudViewModel()
            {
                Code = apartment.Code,
                Building = apartment.Building,
                SelectedState = apartment.State.Id,
                SelectedFloor = apartment.Floor.Id,
                SelectedRoomsNumber = apartment.RoomsNumber.Id,
                SelectedFacade = apartment.Facade.Id,
                ApartmentModel = apartment.Model,
                Price = apartment.Price,
                Area = apartment.Area,
                States = states,
                Floors = floors,
                RoomsNumbers = roomsNumbers,
                Facades = facades
            };

            return View(viewModel);
        }

        //
        // POST: /Admin/EditApartment/5
        [HttpPost]
        public ActionResult EditApartment(int id, ApartmentCrudViewModel model)
        {
            var codeListRep = new CodeListRepository();
            var selectedState = (State)codeListRep.Read(model.SelectedState);
            var selectedFloor = (Floor)codeListRep.Read(model.SelectedFloor);
            var selectedRoomsNumber = (RoomsNumber)codeListRep.Read(model.SelectedRoomsNumber);
            var selectedFacade = (Facade) codeListRep.Read(model.SelectedFacade);

            var apartmentRep = new ApartmentRepository();
            var apartment = apartmentRep.ReadAdmin(id);

            if (ModelState.IsValid)
            {
                apartment.Code = model.Code;
                apartment.Building = model.Building;
                apartment.State = selectedState;
                apartment.Floor = selectedFloor;
                apartment.RoomsNumber = selectedRoomsNumber;
                apartment.Facade = selectedFacade;
                apartment.Model = (int) model.ApartmentModel;
                apartment.Price = (double) model.Price;
                apartment.Area = (double) model.Area;

                apartmentRep.Update(apartment);

                return RedirectToAction("Index");
            }

            var states = codeListRep.ReadStates().ToList();
            
            var floors = codeListRep.ReadFloors().ToList();
            floors.RemoveAt(0);

            var roomsNumbers = codeListRep.ReadRoomsNumber().ToList();
            roomsNumbers.RemoveAt(0);

            var facades = codeListRep.ReadFacades().ToList();

            var viewModel = new ApartmentCrudViewModel()
            {
                Code = apartment.Code,
                Building = apartment.Building,
                SelectedState = apartment.State.Id,
                SelectedFloor = apartment.Floor.Id,
                SelectedRoomsNumber = apartment.RoomsNumber.Id,
                SelectedFacade = apartment.Facade.Id,
                ApartmentModel = apartment.Model,
                Price = apartment.Price,
                Area = apartment.Area,
                Floors = floors,
                Facades = facades,
                States = states,
                RoomsNumbers = roomsNumbers
            };

            return View(viewModel);
        }

        //
        // GET: /Admin/Rooms/Id
        public ActionResult Rooms(int id)
        {
            #region list of rooms

            var apartmentRep = new ApartmentRepository();
            var rooms = apartmentRep.ReadRooms(id);
            var roomViewModel = new RoomCrudViewModel() {Rooms = rooms};

            #endregion

            #region new room

            var codeListRep = new CodeListRepository();
            var types = codeListRep.ReadTypes().ToList();
            roomViewModel.Types = types;

            #endregion

            ViewData["ApartmentId"] = id;
            return View(roomViewModel);
        }

        //
        // POST: /Admin/Rooms/Id
        [HttpPost]
        public ActionResult Rooms(int id, RoomCrudViewModel model)
        {
            var codeListRep = new CodeListRepository();
            var selectedType = (Type)codeListRep.Read(model.SelectedType);

            if (ModelState.IsValid)
            {
                var room = new Room()
                {
                    RoomGroup = (int)model.RoomGroup,
                    Mark = model.Mark,
                    Type = selectedType,
                    Name = model.Name,
                    Area = (double)model.Area
                };

                var apartmentRep = new ApartmentRepository();
                apartmentRep.AddRoom(id, room);

                return RedirectToAction("Rooms", new { id = id });
            }

            var types = codeListRep.ReadTypes().ToList();

            model.Types = types;
            ViewData["ApartmentId"] = id;
            return View(model);
        }

        //
        // GET: /Admin/EditRoom/5/6
        public ActionResult EditRoom(int id, int roomsNumberId)
        {
            var codeListRep = new CodeListRepository();
            var types = codeListRep.ReadTypes().ToList();

            var apartmentRep = new ApartmentRepository();
            var room = apartmentRep.ReadRoom(roomsNumberId);

            var viewModel = new RoomCrudViewModel()
            {
                RoomGroup = room.RoomGroup,
                Mark = room.Mark,
                SelectedType = room.Type.Id,
                Name = room.Name,
                Area = room.Area,
                Types = types
            };

            ViewData["ApartmentId"] = id;
            return View(viewModel);
        }

        //
        // POST: /Admin/EditRoom/5/6
        [HttpPost]
        public ActionResult EditRoom(int id, int roomsNumberId, RoomCrudViewModel model)
        {
            if (ModelState.IsValid)
            {
                var apartmentRep = new ApartmentRepository();
                apartmentRep.UpdateRoom(id, roomsNumberId, model);

                return RedirectToAction("Rooms", new { id = id });
            }

            var codeListRep = new CodeListRepository();
            var types = codeListRep.ReadTypes().ToList();

            model.Types = types;
            return View(model);
        }

        // GET: /Admin/DeleteRoom/5/6
        public ActionResult DeleteRoom(int id, int roomsNumberId)
        {
            ViewData["ApartmentId"] = id;
            return View();
        }

        // POST: /Admin/DeleteRoom/5/6
        [HttpPost]
        public ActionResult DeleteRoom(int id, int roomsNumberId, FormCollection collection)
        {
            var apartmentRep = new ApartmentRepository();
            apartmentRep.DeleteRoom(id, roomsNumberId);

            return RedirectToAction("Rooms", new { id = id });
        }

        // GET: /Admin/GenerateRoom/5/6
        public ActionResult GenerateRoom(int id)
        {
            return View();
        }

        // POST: /Admin/GenerateRoom/5/6
        [HttpPost]
        public ActionResult GenerateRoom(int id, FormCollection collection)
        {
            var apartmentRep = new ApartmentRepository();
            apartmentRep.GenerateRoomsByModel(id);

            return RedirectToAction("Index");
        }
    }
}
